package com.bjc.core.tool.service.impl;

import java.io.File;
import java.util.Map;
import java.util.Map.Entry;

import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import com.bjc.base.utils.StringUtil;
import com.bjc.core.bean.request.EmailRecordReqVO;
import com.bjc.core.bean.request.RichTextReqVO;
import com.bjc.core.bean.response.RichTextVO;
import com.bjc.core.service.IEmailRecordService;
import com.bjc.core.service.IRichTextService;
import com.bjc.core.tool.service.IEmailService;

import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;

@Service
public class EmailImpl implements IEmailService {

	@Autowired
	private JavaMailSender mailSender;
	@Autowired
	private IRichTextService richTextService;
	@Autowired
	private IEmailRecordService emailRecordService;

	@Value("${spring.mail.username}")
	private String sender; // 读取配置文件中的参数

	@Async
	@Override
	public void sendSimpleMail(String TemplateName, Map<String, Object> titleMap, Map<String, Object> contentMap,
			SimpleMailMessage message) throws Exception {
		if (StringUtil.isNullOrEmpty(message.getFrom())) {
			message.setFrom(sender);
		}

		// 获取富文本
		RichTextReqVO richTextReqVO = new RichTextReqVO();
		richTextReqVO.setCode(TemplateName);
		RichTextVO richTextVO = richTextService.get(richTextReqVO);

		Configuration cfg = new Configuration(Configuration.VERSION_2_3_26);
		StringTemplateLoader stringLoader = new StringTemplateLoader();
		stringLoader.putTemplate("titleTemplate", richTextVO.getTitle());
		stringLoader.putTemplate("contentTemplate", richTextVO.getContent());
		cfg.setTemplateLoader(stringLoader);

		// 解析标题模板
		Template titleTemplate = cfg.getTemplate("titleTemplate", "utf-8");
		String title = FreeMarkerTemplateUtils.processTemplateIntoString(titleTemplate, titleMap);
		message.setSubject(title);

		// 解析内容模板
		Template contentTemplate = cfg.getTemplate("contentTemplate", "utf-8");
		String content = FreeMarkerTemplateUtils.processTemplateIntoString(contentTemplate, contentMap);
		message.setText(content);

		mailSender.send(message);
		// 插入发送邮件记录
		EmailRecordReqVO reqVO = new EmailRecordReqVO();
		reqVO.setEmailRecordReqVO(message);
		emailRecordService.insert(reqVO);
	}

	@Async
	@Override
	public void sendMail(String TemplateName, Map<String, Object> titleMap, Map<String, Object> contentMap,
			SimpleMailMessage simpleMailMessage, Map<String, File> attachments) throws Exception {
		MimeMessage message = null;
		message = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
		if (StringUtil.isNullOrEmpty(simpleMailMessage.getFrom())) {
			helper.setFrom(sender);
		}

		helper.setBcc(simpleMailMessage.getBcc());
		helper.setCc(simpleMailMessage.getCc());
		helper.setTo(simpleMailMessage.getTo());
		helper.setReplyTo(simpleMailMessage.getReplyTo());
		helper.setSentDate(simpleMailMessage.getSentDate());

		// 获取富文本
		RichTextReqVO richTextReqVO = new RichTextReqVO();
		richTextReqVO.setCode(TemplateName);
		RichTextVO richTextVO = richTextService.get(richTextReqVO);

		Configuration cfg = new Configuration(Configuration.VERSION_2_3_26);
		StringTemplateLoader stringLoader = new StringTemplateLoader();
		stringLoader.putTemplate("titleTemplate", richTextVO.getTitle());
		stringLoader.putTemplate("contentTemplate", richTextVO.getContent());
		cfg.setTemplateLoader(stringLoader);

		// 解析标题模板
		Template titleTemplate = cfg.getTemplate("titleTemplate", "utf-8");
		String title = FreeMarkerTemplateUtils.processTemplateIntoString(titleTemplate, titleMap);
		helper.setSubject(title);

		// 解析内容模板
		Template contentTemplate = cfg.getTemplate("contentTemplate", "utf-8");
		String content = FreeMarkerTemplateUtils.processTemplateIntoString(contentTemplate, contentMap);
		helper.setText(content);

		// 添加附件
		if (attachments != null && !attachments.isEmpty()) {
			for (Entry<String, File> entry : attachments.entrySet()) {
				FileSystemResource file = new FileSystemResource(entry.getValue());
				helper.addAttachment(entry.getKey(), file);
			}
		}
		mailSender.send(message);
		
		// 插入发送邮件记录
		EmailRecordReqVO reqVO = new EmailRecordReqVO();
		reqVO.setEmailRecordReqVO(simpleMailMessage);
		emailRecordService.insert(reqVO);
	}

}